package com.zhiwang.planet.ui.my.withdraw

import android.graphics.Bitmap
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.zhiwang.planet.R
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.event.AliAccountBindEvent
import com.zhiwang.planet.mvp.p.BindPresenter
import com.zhiwang.planet.mvp.p.GraphCodePresenter
import com.zhiwang.planet.mvp.v.AccountBindView
import com.zhiwang.planet.mvp.v.GraphView
import com.zhiwang.planet.mvp.v.SmsCodeView
import com.zhiwang.planet.mvvm.model.entity.UserDao
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.util.Toaster
import com.zhiwang.planet.util.suicideAfterTransition
import kotlinx.android.synthetic.main.activity_bind_ali_account.*
import org.greenrobot.eventbus.EventBus
import javax.inject.Inject

class BindAliAccountActivity : BaseActivity(), GraphView, SmsCodeView, AccountBindView {

    @Inject
    lateinit var graphPresenter: GraphCodePresenter

    @Inject
    lateinit var userDao: UserDao

    @Inject
    lateinit var bindPresenter: BindPresenter

    var seed = System.currentTimeMillis().toString()
    var phone: String = ""
    override fun initInjection() {
        DaggerUserComponent.builder()
                .accountModule(AccountModule(this))
                .appComponent(getAppComponent())
                .autoDisposeModule(AutoDisposeModule(this))
                .build()
                .inject(this)

        graphPresenter.graphView = this
        bindPresenter.smsCodeView = this
        bindPresenter.bindAccountView = this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bind_ali_account)
        setSupportActionBar(toolbar)
        observeTextChangeWithTargetButtonEnableStatus(
                arrayOf(
                        aliAccount,
                        realName,
                        graphCodeEditor,
                        smsCodeEditor),
                confirmBindBtn)
        getGraphCode(graphCodeBtn)
        phone = userDao.getFirstUser().phone
    }


    fun getGraphCode(v: View) {
        seed = System.currentTimeMillis().toString()
        graphPresenter.getBindGraphCode(seed)
    }

    fun getSmsCode(v: View) {
        val account = aliAccount.text.toString().trim()
        val name = realName.text.toString().trim()
        val graphCode = graphCodeEditor.text.toString().trim()
        if (check(account, name, graphCode))
            bindPresenter.getBindSmsCode(phone, seed, graphCodeEditor.text.toString().trim())
    }

    override fun onError(t: Throwable) {
        getGraphCode(graphCodeBtn)
        super.onError(t)
    }

    override fun onGraphCodeLoad(b: Bitmap) {
        graphCodeBtn.setImageBitmap(b)
    }

    override fun onSmsSend() {
        Toaster.message(R.string.message_sms_send)
        captchaTextView.start()
    }

    fun confirmBindAliAccount(v: View) {
        //ali ,ali name ,smscode, graphcode
        val aliAccount = aliAccount.text.toString()
        val realName = realName.text.toString()
        val smsCode = smsCodeEditor.text.toString()
        val graphCode = graphCodeEditor.text.toString()

        bindPresenter.bindAliAccount(aliAccount, realName, graphCode, smsCode)

    }


    private fun check(account: String, name: String, graphCode: String): Boolean {
        when {
            TextUtils.isEmpty(account) -> {
                Toaster.message(getString(R.string.input_aliaccount_please))
                return false
            }
            TextUtils.isEmpty(name) -> {
                Toaster.message(getString(R.string.input_realname_please))
                return false

            }
            TextUtils.isEmpty(graphCode) -> {
                Toaster.message(getString(R.string.input_graphcode_please))
                return false
            }
        }

        return true
    }


    override fun onAccountBind() {
        EventBus.getDefault().post(AliAccountBindEvent())
        Toaster.message(R.string.ali_bind_success_message)
        suicideAfterTransition()
    }


    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return super.onSupportNavigateUp()
    }
}
